package com.example.collegevice.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.cxs.feignclient.client.CollegeClient;
import com.cxs.feignclient.client.UserClient;
import com.example.collegevice.mapper.CourseMapper;
import com.example.collegevice.service.CourseService;
import com.example.pojo.Course;
import com.example.common.Result;
import com.example.pojo.Teacher;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 课程信息业务层处理
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Autowired
    private UserClient userClient;

    @Autowired
    private CollegeClient collegeClient;

    public void add(Course course) {
        course.setAlreadyNum(0);
        courseMapper.insert(course);
    }

    public PageInfo<Course> selectPage(Course course, Integer pageNum, Integer pageSize) {
        List<Course> list;
        PageHelper.startPage(pageNum, pageSize);
        if (ObjectUtil.isNotEmpty(course.getTeacherId())) {
            Integer teacherId = course.getTeacherId();
            // 表示当前登录的是教师这个角色
            if (ObjectUtil.isNotEmpty(course.getName())) {
                list = courseMapper.selectByNameAndTeacherId(course.getName(), teacherId);
            } else {
                list = courseMapper.selectAllByTeacherId(teacherId);
            }
//            为每一个课程对象添加教师和学院名称
            for (Course c : list) {
                if (ObjectUtil.isNotEmpty(c.getTeacherId())){
//                    Result result1 = userClient.selectTeacherById(teacherId);
//                    Teacher teacher = (Teacher) result1.getData();
//                    c.setTeacherName(teacher.getName());
                    Result result1 = userClient.selectTeacherById(c.getTeacherId());
                    if (result1.getData() instanceof Map) {
                        Map<String, Object> map = (Map<String, Object>) result1.getData();
                        Teacher teacher = new Teacher();
                        teacher.setId((Integer) map.get("id"));
                        teacher.setName((String) map.get("name"));
                        teacher.setSpecialityId((Integer) map.get("specialityId"));
                        c.setTeacherName(teacher.getName());
                    }

                }
                if (ObjectUtil.isNotEmpty(c.getCollegeId()))
                    c.setCollegeName(collegeClient.selectById(c.getCollegeId()).getName());
            }
        } else {
            if (ObjectUtil.isNotEmpty(course.getName())) {
                list = courseMapper.selectByName(course.getName());
            } else {
                list = courseMapper.selectAll();
            }
            //            为每一个课程对象添加教师和学院名称
            for (Course c : list) {
                if (ObjectUtil.isNotEmpty(c.getTeacherId())){
//                    Result result1 = userClient.selectTeacherById(c.getTeacherId());
//                    Teacher teacher = (Teacher) result1.getData();
//                    c.setTeacherName(teacher.getName());
                    Result result1 = userClient.selectTeacherById(c.getTeacherId());
                    if (result1.getData() instanceof Map) {
                        Map<String, Object> map = (Map<String, Object>) result1.getData();
                        Teacher teacher = new Teacher();
                        teacher.setId((Integer) map.get("id"));
                        teacher.setName((String) map.get("name"));
                        teacher.setSpecialityId((Integer) map.get("specialityId"));
                        c.setTeacherName(teacher.getName());
                    }

                }
                if (ObjectUtil.isNotEmpty(c.getCollegeId()))
                    c.setCollegeName(collegeClient.selectById(c.getCollegeId()).getName());
            }
        }
        return PageInfo.of(list);
    }

    public void updateById(Course course) {
        courseMapper.updateById(course);
    }

    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    public List<Course> selectAll() {
        List<Course> list;
        list = courseMapper.selectAll();
        for (Course c : list) {
            if (ObjectUtil.isNotEmpty(c.getTeacherId())){
//                Result result1 = userClient.selectTeacherById(c.getTeacherId());
//                Teacher teacher = (Teacher) result1.getData();
//                c.setTeacherName(teacher.getName());
                Result result1 = userClient.selectTeacherById(c.getTeacherId());
                if (result1.getData() instanceof Map) {
                    Map<String, Object> map = (Map<String, Object>) result1.getData();
                    Teacher teacher = new Teacher();
                    teacher.setId((Integer) map.get("id"));
                    teacher.setName((String) map.get("name"));
                    teacher.setSpecialityId((Integer) map.get("specialityId"));
                    c.setTeacherName(teacher.getName());
                }
            }
            if (ObjectUtil.isNotEmpty(c.getCollegeId()))
                c.setCollegeName(collegeClient.selectById(c.getCollegeId()).getName());
        }
        return list;
    }
}
